package Element.Geom;

import java.awt.*;
import java.awt.geom.*;
import Element.*;
import DistSys.*;

public class GroupGeom extends AbsElemGeom implements Cloneable {

    public GroupGeom() { }

    //its own method
    public void resetNode1AndNode2(){
        int xa=x1,xb=x2, ya=y1, yb=y2;
        x1 = Math.min(xa,xb);
        y1 = Math.min(ya,yb);
        x2 = Math.max(xa,xb);
        y2 = Math.max(ya,yb);

        xa=x1Stored; xb=x2Stored; ya=y1Stored; yb=y2Stored;
        x1Stored = Math.min(xa, xb);
        y1Stored = Math.min(ya, yb);
        x2Stored = Math.max(xa, xb);
        y2Stored = Math.max(ya, yb);
    }

    //overridden methods
     public void calcFrame(){
        frame.width = Math.abs(x2-x1);
        frame.height= Math.abs(y2-y1);
        frame.x = Math.min(x1,x2);
        frame.y = Math.min(y1,y2);
    }

    //not used for checking connectivity which needs stored value
    public boolean coverPoint(int x, int y, int snap){
        double k1,k2;
        Line2D.Float ln = new Line2D.Float();
        for(int i=0;i<4;i++){
            if(      i==0) ln.setLine( x1, y1, x1, y2); // auto cast to 4 floats
            else if( i==1) ln.setLine( x1, y1, x2, y1);
            else if( i==2) ln.setLine( x2, y2, x2, y1);
            else if( i==3) ln.setLine( x2, y2, x1, y2);
            if( snap ==0) {
                if( ln.contains(x,y)== true) return true;
            }
            else {
                if( ln.intersects(x-snap,y-snap, 2*snap, 2*snap)) return true;
            }
        }
        return false;
    }

    //this is used to init when users create a group by drawing in the drawingPanel
    //ATTENTION: node 1, node 2 and frame not initilized;
    //need to be done by programmer after drawing events.
    public void initFromScreen(String n){
        init(n);
        rotation=1; //redundent: rotation never used by Group
    }

    // this is used by reading info from an DB for a Group becoz
    // starting and ending points are all defined in an DB
    public void initFromDB( String n, String desc, int r, int LTC,
                           boolean sn, boolean sd, int[][] si, Point pt1,
                           Point pt2) {
        init(n, desc, r, LTC, sn, sd, si);
        setNode1FromStored( pt1 );
        setNode2FromStored( pt2 );
        resetNode1AndNode2();
        calcFrame();
    }

    public Object clone(){
        GroupGeom created = new GroupGeom();
        this.copyTo( created);// copy AbsElemGeom attributes to created
        return created;
    }
}